package com.tensquare.web.filter;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import org.springframework.stereotype.Component;

@Component
public class WebFilter extends ZuulFilter {

    // pre在程序执行前执行
    // post相反
    @Override
    public String filterType() {
        return "pre";
    }

    // 执行顺序由大到小: 0~99...
    @Override
    public int filterOrder() {
        return 0;
    }

    // 是否执行 true执行 false相反
    @Override
    public boolean shouldFilter() {
        return true;
    }

    /**过滤器的具体逻辑
     * @return 任何Object类型都行(也就是直接null)
     * @throws ZuulException
     */
    @Override
    public Object run() throws ZuulException {
        System.out.println("ZuulFilter执行了!");
        RequestContext context = RequestContext.getCurrentContext();
        String authorization = context.getRequest().getHeader("Authorization");
        if (!authorization.isEmpty()) {
            context.addZuulRequestHeader("Authorization", authorization);
        }
        return null;
    }
}
